<!--
 * @Description: json生成搜素表单模块，不需要表单验证功能
 * @Author: Boide
 * @Date: 2021-09-06 14:23:33
 * @LastEditors: guiyi@hongyi.tech
 * @LastEditTime: 2021-09-13 14:51:08
-->
<template>
  <div>
    <el-form class="HyForm" inline :ref="formRef" :model="formModel" :rules="formRules">
      <template v-for="(item, i) in formConf">
        <el-form-item
          :key="i"
          :prop="item.model"
          :rules="formRules[item.model]"
          :label='item.showName'
        >
          <el-input
            v-if="item.ftype=='text'"
            :placeholder="`请输入${item.showName}`"
            v-model="formModel[item.model]"
            @change="submitForm"
          />
          <el-select
            v-else-if="['switch', 'select'].includes(item.ftype)"
            v-model="formModel[item.model]"
            :placeholder="`全部`"
            :size="item.size"
            @change="submitForm"
          >
            <el-option
              v-for="v in item.options"
              :key="v.id"
              :label="v.value"
              :value="v.id"
            />
          </el-select>
          <el-date-picker
            v-else-if="item.ftype=='dateTime'"
            v-model="formModel[item.model]"
            :type="item.dateType"
            :placeholder="`全部`"
            value-format="yyyy-MM-dd"
            @change="submitForm"
            :clearable="false"
          />
        </el-form-item>
      </template>
      <el-form-item label=''>
        <el-button type="primary" @click="submitForm">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
        <slot name="button"></slot>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'hy-search',
  props: {
    formData: {
      type: Array,
      default() {
        return []
      }
    },
    formRef: {
      type: String,
      default: 'from'+new Date().getTime()
    },
    value: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    if (!this.formData.length) return { formConf: [] }
    const data = {
      formConf: JSON.parse(JSON.stringify(this.formData)),
      formModel: {},
      formRules: {},
      formActive: false
    }
    this.initFormData(data.formConf, data.formModel)
    // this.initFormRules(data.formConf, data.formRules)
    return data
  },
  watch: {
    value(newVal) {
      this.formModel = newVal
    }
  },
  methods: {
    submitForm() {
      this.$emit('input', this.formModel)
      this.$emit('search', this.formModel)
    },
    resetQuery() {
      this.$emit('input', {})
      this.$emit('search', {})
    },
    initFormData(componentList = [], formData) {
      componentList.forEach(cur => {
        // const config = cur.config
        if (cur.model) formData[cur.model] = ''
      })
    },
    initFormRules(componentList = [], formData) {
      componentList.forEach(cur => {
        const config = cur.config
        if (cur.model) formData[cur.model] = config.rulesList
      })
    }
  }
}
</script>

<style scoped  lang="scss">
</style>
